#include "TDefAlgorithm.h"

TDefAlgorithm::TDefAlgorithm(void)
{
}

TDefAlgorithm::~TDefAlgorithm(void)
{
}
const char * TDefAlgorithm::find_area_name( DWORD ip , VEC_IPS & vec_start , VEC_IPS & vec_end ){
	if ( vec_start.size() <= 0 ) return NULL ;

	int start =0;
	int end = vec_start.size()-1;
	int middle = (start+end)/2;
	IPLocation * find_ip  = NULL;
	find_ip = vec_start[start];
	if (find_ip->start_int <= ip && ip <= find_ip->end_int)
	{
		return find_ip->county;
	}
	find_ip = vec_start[end];
	if (find_ip->start_int <= ip && ip <= find_ip->end_int)
	{
		return find_ip->county;
	}
	while(start <= end)
	{
		find_ip  = vec_start[middle];
		if (end - start == 1)
		{
			find_ip  = vec_start[start];
			if (find_ip->start_int <= ip && ip <= find_ip->end_int)
			{
				return find_ip->county;
			}
			find_ip  = vec_start[end];
			if (find_ip->start_int <= ip && ip <= find_ip->end_int)
			{
				return find_ip->county;
			}
			break;
		}
		if (find_ip->start_int <= ip && ip <= find_ip->end_int)
		{
			return find_ip->county;
		}
		else if (find_ip->start_int > ip)
		{
			end = middle;
		}
		else if (find_ip->end_int <ip)
		{
			start = middle;
		}
		middle = (start+end)/2;
	}
	start =0;
	end = vec_end.size()-1;
	middle = (start+end)/2;
	find_ip = vec_end[start];
	if (find_ip->start_int <= ip && ip <= find_ip->end_int)
	{
		return find_ip->county;
	}
	find_ip = vec_end[end];
	if (find_ip->start_int <= ip && ip <= find_ip->end_int)
	{
		return find_ip->county;
	}
	while(start <= end)
	{
		find_ip  = vec_end[middle];
		if (end - start == 1)
		{
			find_ip  = vec_end[start];
			if (find_ip->start_int <= ip && ip <= find_ip->end_int)
			{
				return find_ip->county;
			}
			find_ip  = vec_end[end];
			if (find_ip->start_int <= ip && ip <= find_ip->end_int)
			{
				return find_ip->county;
			}
			break;
		}
		if (find_ip->start_int <= ip && ip <= find_ip->end_int)
		{
			return find_ip->county;
		}
		else if (find_ip->start_int > ip)
		{
			end = middle;
		}
		else if (find_ip->end_int <ip)
		{
			start = middle;
		}
		middle = (start+end)/2;
	}
	return NULL;
}
